﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_MaterialOrderDetail : Form
    {
        public string order_number;

        private INISettings mySetting;
        private SqlConnection myConnection;

        public frm_Add_MaterialOrderDetail()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            this.order_number = null;

            LoadMaterial();

            this.materialID.SelectedIndexChanged += materialID_SelectedIndexChanged;
            
        }


        ////////////////////////////////////////////
        /// When change Material
        ///////////////////////////////////////////
        void materialID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboboxItem item = (ComboboxItem)this.materialID.SelectedItem;
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT RoHS_RRI, Price, QuantityPerUnit"
                    + " FROM [444_Material] WHERE ID=" + item.Value.ToString();
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                if (dataReader.Read())
                {
                    this.roHS.Text = dataReader["RoHS_RRI"].ToString();
                    this.priceValue.Text = dataReader["Price"].ToString();
                    this.quantityUnit.Text = dataReader["QuantityPerUnit"].ToString();
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }

                MessageBox.Show("Cannot get Material List");
            }
        }
        
        //////////////////////////////////////////////
        /// Load Material List
        //////////////////////////////////////////////
        private void LoadMaterial()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT ID, PartNo FROM [444_Material] ORDER BY PartNo";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["PartNo"].ToString();
                    item.Value = dataReader["ID"].ToString();

                    this.materialID.Properties.Items.Add(item);
                }                
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }

                MessageBox.Show("Cannot get Material information");
            }
        }

        /////////////////////////////////////
        /// Set Order No
        ////////////////////////////////////
        public void setOrderNo(string value)
        {
            order_number = value;
            this.orderNo.Text = value;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;
            DateTime dead_line = new DateTime();
            int quantity = 0;
            string qty = null;
            ComboboxItem item = new ComboboxItem();

            if (this.materialID.SelectedIndex < 0)
            {
                error = "Please select material";
            }
            else
            {
                item = (ComboboxItem)this.materialID.SelectedItem;
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.deadLine.EditValue != null)
                {
                    dead_line = DateTime.Parse(this.deadLine.EditValue.ToString());
                }

                if (!string.IsNullOrEmpty(this.quantityValue.Text))
                {
                    if (Int32.TryParse(this.quantityValue.Text, out quantity))
                    {
                        qty = quantity.ToString();
                    }
                }

                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [444_MaterialOrderDetail] (OrderNo, MaterialID, Quantity, DeadLine)"
                        + " VALUES ('" + order_number + "', '" + item.Value.ToString() + "', " + qty + ", @deadline)";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@deadline", SqlDbType.DateTime).Value = dead_line.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Material Orther Detail added success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Material Order Detail. Please try again.");
                }
            }

            if (!string.IsNullOrEmpty(error))
            {
                MessageBox.Show(error);
            }
        }
    }
}
